package com.example.wanghao.wangyixinwen.fragment;

import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.example.wanghao.wangyixinwen.R;
import com.example.wanghao.wangyixinwen.act.MainActivity;
import com.example.wanghao.wangyixinwen.home.BasePage;
import com.example.wanghao.wangyixinwen.home.FunctionPage;
import com.example.wanghao.wangyixinwen.home.GoverPage;
import com.example.wanghao.wangyixinwen.home.NewCenterPage;
import com.example.wanghao.wangyixinwen.home.SettingPage;
import com.example.wanghao.wangyixinwen.home.SmartServicePage;
import com.example.wanghao.wangyixinwen.view.LazyViewPager;
import com.example.wanghao.wangyixinwen.view.NoScroViewPager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanghao on 2016/5/18.
 */
public class HomeFragment extends BaseFragment implements LazyViewPager.OnPageChangeListener {

    private NoScroViewPager mMViewPager;
    private List<BasePage> mHomePages;//主界面的页面集合
    //   private Context mContext;
    private RadioGroup mMain_radio;

  /*  //初始化方法
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }
*/
   /* //界面初始化方法
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_home, null);
        mMViewPager = (ViewPager) view.findViewById(R.id.view_pager);
        mMain_radio = (RadioGroup) view.findViewById(R.id.main_radio);
        //mMViewPager.setAdapter();
        return view;
    }*/

    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.frag_home, null);
        mMViewPager = (NoScroViewPager) view.findViewById(R.id.view_pager);
        mMain_radio = (RadioGroup) view.findViewById(R.id.main_radio);
        // ViewUtils.inject(this,view);//注入
        return view;
    }

    /*//数据初始化方法
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mHomePages = new ArrayList<>();
        mHomePages.add(new FunctionPage(mContext));//首页
        mHomePages.add(new NewCenterPage(mContext));//新闻中心
        mHomePages.add(new SmartServicePage(mContext));//智慧服务
        mHomePages.add(new GoverPage(mContext));//政务指南
        mHomePages.add(new SettingPage(mContext));//设置

        HomeAdapter homeAdapter = new HomeAdapter();
        mMViewPager.setAdapter(homeAdapter);

        //设置单选按钮点击事件
        mMain_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //设置侧滑菜单
                SlidingMenu slidingMenu = ((MainActivity) mContext).getSlidingMenu();

                switch (checkedId){
                    case R.id.rb_function://首页
                        mMViewPager.setCurrentItem(0,false);//让viewpager不滑动,直接切换界面
                       //设置侧滑菜单禁止显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                        break;
                    case R.id.rb_news_center://新闻中心
                        mMViewPager.setCurrentItem(1,false);//让viewpager不滑动,直接切换界面
                       //设置侧滑菜单显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        break;
                    case R.id.rb_smart_service://智慧服务
                        mMViewPager.setCurrentItem(2,false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        break;
                    case R.id.rb_gov_affairs://政务指南
                        mMViewPager.setCurrentItem(3,false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        break;
                    case R.id.rb_setting://设置
                        mMViewPager.setCurrentItem(4,false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单禁止显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                        break;
                }
            }
        });
        //第一次进入的时候让首页默认选中
        mMain_radio .check(R.id.rb_function);

    }*/

    @Override
    protected void initData() {
        mHomePages = new ArrayList<>();
        mHomePages.add(new FunctionPage(mContext));//首页
        mHomePages.add(new NewCenterPage(mContext));//新闻中心
        mHomePages.add(new SmartServicePage(mContext));//智慧服务
        mHomePages.add(new GoverPage(mContext));//政务指南
        mHomePages.add(new SettingPage(mContext));//设置

        HomeAdapter homeAdapter = new HomeAdapter();
        mMViewPager.setAdapter(homeAdapter);

        //设置单选按钮点击事件
        mMain_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
               //点击单选按钮时,给菜单界面设置对应的类型,来决定是显示哪一个listview
                MenuFragment2 menuFragment2 = ((MainActivity) mContext).getMenuFragment();
                //设置侧滑菜单
                SlidingMenu slidingMenu = ((MainActivity) mContext).getSlidingMenu();

                switch (checkedId) {
                    case R.id.rb_function://首页
                        mMViewPager.setCurrentItem(0, false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单禁止显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                        break;
                    case R.id.rb_news_center://新闻中心
                        mMViewPager.setCurrentItem(1, false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        //让新闻中心进行数据初始化操作
                        menuFragment2.setMenuType(MenuFragment2.NEWCENTER);
                        mHomePages.get(1).onResume();
                      /*  NewCenterPage newCenterPage = new NewCenterPage(mContext);

                        newCenterPage.initData();*/
                       /* NewCenterPage newCenterPage = (NewCenterPage) mHomePages.get(1);
                        newCenterPage.initData();*/
                        break;
                    case R.id.rb_smart_service://智慧服务
                        mMViewPager.setCurrentItem(2, false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                       menuFragment2.setMenuType(menuFragment2.SMARTSERVICE);
                        break;
                    case R.id.rb_gov_affairs://政务指南
                        mMViewPager.setCurrentItem(3, false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        menuFragment2.setMenuType(menuFragment2.GOVER);
                        break;
                    case R.id.rb_setting://设置
                        mMViewPager.setCurrentItem(4, false);//让viewpager不滑动,直接切换界面
                        //设置侧滑菜单禁止显示
                        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                        break;
                }
            }
        });
        //第一次进入的时候让首页默认选中
        mMain_radio.check(R.id.rb_function);

        mMViewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        BasePage basePage = mHomePages.get(position);
        //判断是否已经获取过数据了,如果已经获取,就不再重新调用initdata
        if (!basePage.isLoad) {
            basePage.initData();
            //         basePage.isLoad = true;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private class HomeAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mHomePages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mHomePages.get(position).getRootView());
            return mHomePages.get(position).getRootView();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    public NewCenterPage getNewCenter() {
        NewCenterPage newCenterPage = (NewCenterPage) mHomePages.get(1);

        return newCenterPage;
    }
}
